/*
 * Copyright kecp
 * Apr 24, 2010
 */

package LBS;


/**
 * The Cell calss is used to get network information such as cellid, mnc ,lac and
 * mcc from a mobile.
 * @author kecp
 */
public class Cell {
    public String cell_id;
    public String mnc;
    public String mcc;
    public String lac;
  
    public Cell(){
         cell_id = mnc = mcc = lac = null;
 
       //  Tool.Debugger.addDebugInfo( System.getProperty("microedition.platform") );
    }

    private String strNullToEmpty( String str ){
        if( str == null )
            return "";
        else
            return str;
    }

    /**
     *
     * @param another 基站
     * @return 两个基站信息是否一致
     */
    public boolean isEquals( Cell another ) {
        if( this == another )
            return true;

        else if( cell_id.equals(another.cell_id) && mnc.equals( another.mnc)
                && mcc.equals(another.mcc ) && lac.equals(another.lac) )
            return true;
        return false;
    }

    /**
     *
     * @return 当前的基站信息是否有效
     */
    public boolean isAvailable() {
        if( cell_id == null || cell_id.equals("") )
            return false;
        return true;
    }

    /**
     * update with sonyericsson phones
     */
    public void updateSE() {
       try {
            cell_id = System.getProperty("com.sonyericsson.net.cellid");
            lac = System.getProperty("com.sonyericsson.net.lac");
            mcc = System.getProperty("com.sonyericsson.net.cmcc");
            mnc = System.getProperty("com.sonyericsson.net.cmnc");
       } catch(Exception e) {
            e.printStackTrace();
       }
    }

    /**
     * update with nokia phones
     */
    protected void updateNokia() {
        cell_id = mnc = mcc = lac = null;
        //s40
        cell_id = System.getProperty("Cell-ID");
        //s60
        if( cell_id == null || cell_id.equals("") )
            cell_id = System.getProperty("com.nokia.mid.cellid");

        //On Series 40 3rd Edition, Feature Pack 1 and later:
        mnc = System.getProperty("com.nokia.mid.mnc");
        if( mnc == null || mnc.equals("") )
            mnc = System.getProperty("com.nokia.mid.networkID");
         //Getting MNC is supported on S60 3rd edition FP2 and later:
        else if( mnc == null || mnc.equals("") )
            mnc = System.getProperty("com.nokia.mid.networkid");

        mcc = System.getProperty("com.nokia.mid.countrycode");
        lac = System.getProperty("com.nokia.mid.lac");
     //   System.getProperty("com.nokia.mid.networksignal")
    }

    /**
     * get the current network of the mobile
     * @return false is the mobile isn't supported.
     */
    public boolean updateNetworkInfo(){
        updateNokia();
        if( cell_id == null || cell_id.equals("") )
            updateSE();

        cell_id = strNullToEmpty( cell_id );
        mnc = strNullToEmpty( mnc );
        mcc = strNullToEmpty( mcc );
        lac = strNullToEmpty( lac );


        //@test
//        cell_id =  "42463";
//        mcc = "240";
//        mnc = "08";
//        lac = "017c";
        //

        return isAvailable();
    }
    public String toString(){
        String str;
        str = "cell_id:"+cell_id+"\nmnc:"+mnc+"\nmcc"+mcc+"\nlac"+lac;
        return str;
    }


   
    
}
